package com.wf.ew.orderB.biz.impl;

import com.wf.ew.JTServer.common.packet.JT809Packet0x1202;
import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.orderB.biz.CarGpsBiz;
import com.wf.ew.orderB.model.CarGps;
import com.wf.ew.system.model.User;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-04-13 13:55:07
 * @version ：1.0
 */
@Repository
public class CarGpsBizImpl extends BaseDao implements CarGpsBiz {

	@Override
	public String list(PageResult<T> page, CarGps carGps, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<CarGps> rm = new BeanPropertyRowMapper<CarGps>(CarGps.class);
		StringBuffer sql = new StringBuffer("select c.id,c.companyId,c.vehicleno");
        sql.append(",c.lon,c.lat,c.sendtime,c.cjsj");
		sql.append(",c.vec1,c.vec2,c.vec3,c.direction");
		sql.append(",c.altitude,c.alarm,c.createTime,c.createUser");
		sql.append(",c.updateTime,c.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_cargps c ");
        sql.append(" inner join sys_user suser on suser.id=c.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
        sql.append(" where c.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and c.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(carGps)){
//			if(Utils.isNotEmpty(carGps.getName())){
//				sql.append(" and instr(c.name,:name)>0 ");
//				map.put("name", carGps.getName());
//			}
			if(Utils.isNotEmpty(carGps.getSort())&&Utils.isNotEmpty(carGps.getOrder())){
				sql.append(" order by "+carGps.getSort()+" "+carGps.getOrder());
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<CarGps> list(CarGps carGps, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<CarGps> rm = new BeanPropertyRowMapper<CarGps>(CarGps.class);
        StringBuffer sql = new StringBuffer("select c.id,c.companyId,c.vehicleno");
        sql.append(",c.lon,c.lat,c.sendtime,c.cjsj");
		sql.append(",c.vec1,c.vec2,c.vec3,c.direction");
		sql.append(",c.altitude,c.alarm,c.createTime,c.createUser");
		sql.append(",c.updateTime,c.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_cargps c ");
        sql.append(" inner join sys_user suser on suser.id=c.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
        sql.append(" where c.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and c.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(carGps)){
//			if(Utils.isNotEmpty(carGps.getName())){
//				sql.append(" and instr(c.name,:name)>0 ");
//				map.put("name", carGps.getName());
//			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}


	@Override
	public List<CarGps> selectLonLat(CarGps carGps) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<CarGps> rm = new BeanPropertyRowMapper<CarGps>(CarGps.class);
        StringBuffer sql = new StringBuffer("select c.lon,c.lat ");
        sql.append(" from sszg_cargps c ");
        sql.append(" where 1=1 ");
    	if(Utils.isNotEmpty(carGps)){
			if(Utils.isNotEmpty(carGps.getVehicleno())){
				sql.append(" and c.vehicleno = :vehicleno ");
				map.put("vehicleno", carGps.getVehicleno());
			}
		}
		sql.append(" order by sendtime desc limit 1 ");
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public CarGps getById(CarGps carGps) {
		RowMapper<CarGps> rm = new BeanPropertyRowMapper<CarGps>(CarGps.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select c.id,c.companyId,c.vehicleno");
        sql.append(",c.lon,c.lat,c.sendtime,c.cjsj");
		sql.append(",c.vec1,c.vec2,c.vec3,c.direction");
		sql.append(",c.altitude,c.alarm,c.createTime,c.createUser");
		sql.append(",c.updateTime,c.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_cargps c ");
        sql.append(" inner join sys_user suser on suser.id=c.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
        sql.append(" where c.del=0 and cmy.del=0 and c.id=:id ");
		map.put("id", carGps.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(CarGps carGps, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sszg_cargps (");
        sql.append("companyId,vehicleno,lon,lat");
		sql.append(",sendtime,cjsj,vec1,vec2");
		sql.append(",vec3,direction,altitude,alarm");
		sql.append(",createUser) values ( ");
        sql.append(":companyId,:vehicleno,:lon,:lat");
		sql.append(",:sendtime,:cjsj,:vec1,:vec2");
		sql.append(",:vec3,:direction,:altitude,:alarm");
		sql.append(",:createUser)");
        map.put("companyId", loginUser.getCompanyId());
        map.put("vehicleno", carGps.getVehicleno());
        map.put("lon", carGps.getLon());
        map.put("lat", carGps.getLat());
        map.put("sendtime", carGps.getSendtime());
        map.put("cjsj", carGps.getCjsj());
        map.put("vec1", carGps.getVec1());
        map.put("vec2", carGps.getVec2());
        map.put("vec3", carGps.getVec3());
        map.put("direction", carGps.getDirection());
        map.put("altitude", carGps.getAltitude());
        map.put("alarm", carGps.getAlarm());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(CarGps carGps, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sszg_cargps set ");
        sql.append("vehicleno=:vehicleno,lon=:lon,lat=:lat ");
		sql.append(",sendtime=:sendtime,cjsj=:cjsj,vec1=:vec1 ");
		sql.append(",vec2=:vec2,vec3=:vec3,direction=:direction ");
		sql.append(",altitude=:altitude,alarm=:alarm,updateTime=now() ");
		sql.append(",updateUser=:updateUser where id=:id ");
        map.put("vehicleno", carGps.getVehicleno());
        map.put("lon", carGps.getLon());
        map.put("lat", carGps.getLat());
        map.put("sendtime", carGps.getSendtime());
        map.put("cjsj", carGps.getCjsj());
        map.put("vec1", carGps.getVec1());
        map.put("vec2", carGps.getVec2());
        map.put("vec3", carGps.getVec3());
        map.put("direction", carGps.getDirection());
        map.put("altitude", carGps.getAltitude());
        map.put("alarm", carGps.getAlarm());
		map.put("updateUser", loginUser.getId());
		map.put("id", carGps.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(CarGps carGps, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(carGps.getIds().length()>0){
			for(String ID : carGps.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update sszg_cargps set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}

	@Override
	public boolean add2(JT809Packet0x1202 packet) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sszg_cargps (");
		sql.append("vehicleno,lon,lat,sendtime");
		sql.append(",cjsj,vec1,vec2,vec3,direction");
		sql.append(",altitude,alarm,createUser) values ( ");
		sql.append(":vehicleno,:lon,:lat,:sendtime");
		sql.append(",:cjsj,:vec1,:vec2,:vec3,:direction");
		sql.append(",:altitude,:alarm,:createUser)");
		map.put("vehicleno", packet.getVehicleNo().replaceAll("\u0000", ""));
		map.put("lon", String.format("%.6f", packet.getLon() * 1e-6));
		map.put("lat", String.format("%.6f", packet.getLat() * 1e-6));
		map.put("sendtime", LocalDateTime.of(packet.getDate(), packet.getTime()).toString());
		map.put("cjsj", LocalDateTime.now().toString());
		map.put("vec1", packet.getVec1()+"");
		map.put("vec2", packet.getVec2()+"");
		map.put("vec3", packet.getVec3()+"");
		map.put("direction", packet.getDirection()+"");
		map.put("altitude", packet.getAltitude()+"");
		map.put("alarm", packet.getAlarm()+"");
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete2(JT809Packet0x1202 packet) {
		int count = 0;
		Map<String, String> map = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("DELETE FROM sszg_cargps WHERE vehicleno = ：vehicleno ");
		map.put("vehicleno", packet.getVehicleNo().replaceAll("\u0000", ""));
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(),map);
		return count > 0;
	}
	@Override
	public int delYesterdayData() {
		int count = 0;
		Map<String, String> map = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("DELETE FROM sszg_khkas.sszg_cargps WHERE sendtime < CURRENT_DATE");
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(),map);
		return count;
	}
}
